import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class UpdateStudentRecord extends HttpServlet
{
	private String sql, url;
	private Connection conn;
	private ResultSet rs;
	private Statement stmt;
	private PreparedStatement ps;
	
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException 
    {	
    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();

    	out.print("<html><body>");
    	
    	String id = req.getParameter("id");
		
    	try 
    	{
    		Class.forName("com.mysql.jdbc.Driver");
    		url = "jdbc:mysql://localhost/student_db";
    		conn = DriverManager.getConnection(url, "root", "");
    		
    		stmt = conn.createStatement();
    		rs = stmt.executeQuery("Select * FROM student WHERE id =" + id);
    		
    		while(rs.next())
    		{
   			
    		out.print("<form action=\"");
    		out.print( req.getRequestURI() );
    		out.print("\" method=\"post\">");
    		out.print("<input type=\"hidden\" name=\"id\" value=\"" + rs.getObject(1).toString() + "\"><br>");
    		out.print("Name :");
    		out.print("<input type=\"text\" name=\"name\" value=\"" + rs.getObject(2).toString() + "\"><br>");
    		out.print("Address :");
    		out.print("<input type=\"text\" name=\"address\" value=\"" + rs.getObject(3).toString() + "\"><br>");
    		out.print("Telephone :");
    		out.print("<input type=\"text\" name=\"telephone\" value=\"" + rs.getObject(4).toString() + "\"><br>");
    		out.print("<br><br><input type=\"submit\" value=\"Update Student\">");
    		out.print("<input type=\"button\" onClick=\"parent.location='Main'\" value=\"Cancel\">");
    		
    		}
    	} 
    	catch (ClassNotFoundException cnfe) 
    	{
    			out.println("" + cnfe);
    	}
    	catch (SQLException sqle)
    	{
    			out.println("" + sqle);
    	}
    	finally
    	{
    		try
    		{
    			if(conn != null)
    			{
    				conn.close();
    			}
    		}
    		catch(SQLException sqle)
    		{
    			out.println("" + sqle);
    		}
    	}
    	
    	out.print("</body></html>");
		out.close();
    	
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException 
    {	
    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();

    	out.print("<html><body>");
    	
    	String id = req.getParameter("id").trim();
    	String name = req.getParameter("name").trim();
		String address = req.getParameter("address").trim();
		String telephone = req.getParameter("telephone").trim();
		boolean proceed = false;
		
		if(name != null && address != null && telephone != null)
			if(name.length() > 0 && address.length() > 0 && telephone != null)
				proceed = true;
		
    	try 
    	{
    		Class.forName("com.mysql.jdbc.Driver");
    		url = "jdbc:mysql://localhost/student_db";
    		conn = DriverManager.getConnection(url, "root", "");
    	
    		sql = ("UPDATE student SET name=?, address=?, phone=? WHERE id =" + id);
    		ps = conn.prepareStatement(sql);
    		
    		if(proceed)
    		{
    			ps.setString(1, name);
    			ps.setString(2, address);
    			ps.setString(3, telephone);
    			ps.executeUpdate();
    		}
    		
    		String contextPath= "http://localhost:8080/Student";
    	    res.sendRedirect(res.encodeRedirectURL(contextPath + "/Main"));
    	
    	} 
    	catch (ClassNotFoundException cnfe) 
    	{
    			out.println("" + cnfe);
    	}
    	catch (SQLException sqle)
    	{
    			out.println("" + sqle);
    	}
    	finally
    	{
    		try
    		{
    			if(ps != null)
    			{
    				ps.close();
    			}
    			if(conn != null)
    			{
    				conn.close();
    			}
    		}
    		catch(SQLException sqle)
    		{
    			out.println("" + sqle);
    		}
    	}
    	
    	out.print("</body></html>");
		out.close();
		
		
    	
    }
}